package com.itheima.spring.bestpractive.controller.emp;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.spring.bestpractive.common.myResult.R;
import com.itheima.spring.bestpractive.pojo.*;
import com.itheima.spring.bestpractive.service.AssessmentService;
import com.itheima.spring.bestpractive.service.EmpService;
import com.itheima.spring.bestpractive.utils.TokenForId;
import com.itheima.spring.bestpractive.vo.resp.assessment.AssessmentPlanRespVo;
import com.itheima.spring.bestpractive.vo.resp.assessment.AssessmentIndicatorRespVo;
import com.itheima.spring.bestpractive.vo.resp.employee.EmpRespVo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author: HZY
 * @date: 2025/6/25 16:07
 * @Content:
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private AssessmentService assessmentService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EmpService empService;

    @GetMapping("/getAssessmentPlan")
    @Operation(description = "获取个人的全部考核计划")
    public R getAssessmentIndicator(@RequestParam("employeeId") long employeeId) {
        List<AssessmentPlan> assessmentPlanList = assessmentService.getAssessmentPlanList(employeeId);
        System.out.println(assessmentPlanList);
        return R.success(assessmentPlanList);
    }

    @GetMapping("/getAssessmentPlan/getAssessmentIndicator")
    @Operation(description = "获取考核指标和对应分数")
    public R getAssessmentIndicator(@RequestParam long planId, @RequestParam long empId) {
        System.out.println("planId = " + planId + " empId = " + empId);
        List<AssessmentIndicatorRespVo> indicatorByPId = assessmentService.getIndicatorByPId(planId, empId);
        AssessmentResult assessmentResult = assessmentService.getAssessmentResult(empId, planId);
        Map<String, Object> map = new HashMap<>();
        map.put("indicators", indicatorByPId);
        map.put("result", assessmentResult);
        return R.success(map);
    }

    @PutMapping("/PersonalIndicatorStatus")
    @Operation(description = "更改成绩状态，发送员工编号，考个计划编号，状态，评价")
    public R updateIndicatorStatus(@RequestBody Map<String, Object>  data){
        empService.updatePlanStatus(data);
        return R.success();
    }

    @GetMapping("/getAssessmentIndicators")
    @Operation(description = "获取考核指标")
    public R getAssessmentIndicators(@RequestParam long planId) {
        AssessmentPlan plan = new AssessmentPlan();
        plan.setPlanId(planId);
        List<AssessmentIndicator> assessmentIndicators = assessmentService.getAssessmentIndicatorList(plan);
        return R.success(assessmentIndicators);
    }

    @GetMapping("/PersonalInformation")
    @Operation(description = "获取个人信息")
    public R getPersonalInformation(long employeeId) throws JsonProcessingException {
        System.out.println(employeeId);
        EmpRespVo empRespVo = empService.getEmpFromCache(employeeId);
        return R.success(empRespVo);
    }

    @PutMapping("/updataResultStatus")
    @Operation(description = "修改员工考核结果状态")
    public R updataResultStatus(@RequestBody Map<String, Object>  data){
        //信息包括员工编号，计划编号，状态
        assessmentService.updateResultStatus(data);
        return R.success();
    }

    @Operation(description = "保存异议信息")
    @PostMapping("/saveDisputes")
    public R saveDisputes(@RequestBody Map<String, Object>  disputes){
        System.out.println(disputes);
        //信息包括，员工编号，计划编号，异议内容
        assessmentService.saveDisputes(disputes);
        return R.success();
    }
}
